﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using YnPlcDataServer.Entities;

namespace YnPlcDataServer
{
    public partial class HistoryDataChartForm : Form
    {
        #region 自定义变量
        public List<YnSvid> listYnSvid  { get; set; }
        public List<YnSvData> listYnSvData { get; set; }
        #endregion

        #region 窗体
        public HistoryDataChartForm()
        {
            InitializeComponent();
        }
        private void HistoryDataChartForm_Load(object sender, EventArgs e)
        {

        }
        private void HistoryDataChartForm_Shown(object sender, EventArgs e)
        {
            InitChart();
            try
            {
                foreach(YnSvid ynSvid  in listYnSvid)
                {
                    AddChart(ynSvid);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示...", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void button_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 图形
        private void InitChart()
        {


        }
        private void AddChart(YnSvid ynSvid)
        {

            try
            {

                Chart chart1 = new Chart();
                chart1.Tag = ynSvid;
                ChartArea chartArea1 = new ChartArea();
                Legend legend1 = new Legend();
                Title title1 = new Title();

                chartArea1.AxisX.LabelStyle.Format = "HH:mm";//:ss
                //设置间隔类型           
                chartArea1.AxisX.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Minutes;
                chartArea1.AxisX.Interval = 5;
                //chartArea1.AxisX.MajorGrid.Interval = 0D;
                //chartArea1.AxisX.Maximum = 43636.684328703705D;
                //chartArea1.AxisX.Minimum = 43636.64266203704D;
                chartArea1.AxisX.Title = "时间";

                //chartArea1.AxisY.Interval = 10D;
                //chartArea1.AxisY.MajorGrid.Interval = 30D;
                //chartArea1.AxisY.Maximum = 100D;
                chartArea1.AxisY.Minimum = 0D;
                chartArea1.AxisY.Title = "值";
                //chartArea1.Name = "ChartArea1";
                chart1.ChartAreas.Add(chartArea1);

                Series seriesValue = new Series();
                //seriesValue.ChartArea = "ChartArea1";
                seriesValue.ChartType = SeriesChartType.FastLine;
                //seriesValue.XValueType = ChartValueType.Time;
                seriesValue.XValueType = ChartValueType.DateTime;
                //seriesValue.Legend = "Legend1";
                //seriesValue.Name = "Series1";
                seriesValue.LegendText = ynSvid.name;
                chart1.Series.Add(seriesValue);

                Series seriesUpper = new Series();
                seriesUpper.ChartType = SeriesChartType.Line;
                seriesUpper.XValueType = ChartValueType.DateTime;
                seriesUpper.LegendText = "规格上限";
                seriesUpper.Color = Color.Red;
                chart1.Series.Add(seriesUpper);

                Series seriesLower = new Series();
                seriesLower.ChartType = SeriesChartType.Line;
                seriesLower.XValueType = ChartValueType.DateTime;
                seriesLower.LegendText = "规格下限";
                seriesLower.Color = Color.FromArgb(192, 0, 0);//.Red;//192, 0, 0
                chart1.Series.Add(seriesLower);

                chart1.Legends.Add(legend1);
                //chart1.Location = new System.Drawing.Point(0, 0);
                //chart1.Name = "chart2";
                chart1.Size = new System.Drawing.Size(930, 420);
                chart1.TabIndex = 0;
                //chart1.Text = "chart2";

                title1.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F);
                //title1.Name = "Title_Main";
                title1.Text = ynSvid.name;
                chart1.Titles.Add(title1);

                this.panel_chart.Controls.Add(chart1);
                chart1.BringToFront();
                chart1.Dock = System.Windows.Forms.DockStyle.Top;

                List<YnSvData> _listYnSvData = listYnSvData.Where(item => item.svid_id == ynSvid.svid_id).ToList();

                if(_listYnSvData.Count>0)
                {
                    DateTime dtStart = _listYnSvData[0].createTime;
                    DateTime dtEnd = _listYnSvData[_listYnSvData.Count - 1].createTime;
                    double dEnd = dtEnd.ToOADate();
                    double dStart = dtStart.ToOADate();
                    chart1.ChartAreas[0].AxisX.Minimum = dStart;
                    chart1.ChartAreas[0].AxisX.Maximum = dEnd;

                    foreach (YnSvData ynSvData in _listYnSvData)
                    {
                        //增加点
                        seriesValue.Points.AddXY(ynSvData.createTime.ToOADate(), ynSvData.value);
                        seriesUpper.Points.AddXY(ynSvData.createTime.ToOADate(), ynSvid.upper_spec_limit);
                        seriesLower.Points.AddXY(ynSvData.createTime.ToOADate(), ynSvid.lower_spec_limit);
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示...", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
        #endregion


    }
}
